#include <string>
#include <boost/lexical_cast.hpp>

#include "include/MysqlProxy.h"
#include "include/Configuration.h"

using namespace std;

int MysqlProxy::Init()
{
	if(!_conn.connect(
		Configuration::db.name.c_str(), Configuration::db.server.c_str(),
		Configuration::db.user.c_str(), Configuration::db.passwd.c_str(),
		Configuration::db.port))
	{
		// test
		cout << "init mysql failed" << endl;

		return 1;
	}

	_query_error = 0;

	return 0;
}

int MysqlProxy::BeginTransaction()
{
	_p_transaction = new mysqlpp::Transaction(_conn);
	if(NULL == _p_transaction)
	{
	    return 1;
	}

	return 0;
}

void MysqlProxy::RollBack()
{
	if(!_p_transaction)
	{
		return;
	}

	_p_transaction->rollback();

	delete _p_transaction;
	_p_transaction = NULL;
}

void MysqlProxy::Commit()
{
	if(!_p_transaction)
	{
		return;
	}

	_p_transaction->commit();

	delete _p_transaction;
	_p_transaction = 0;
}

int MysqlProxy::Exec(string sql)
{
	try
	{
		mysqlpp::Query query = _conn.query(sql);
		if(query.exec())
		{
		    return 0;
		}
		else
		{
		    return 1;
		}
	}
	catch(mysqlpp::Exception &exception)
	{
		return 1;
	}

	return 0;
}

mysqlpp::StoreQueryResult MysqlProxy::DoQuery(string sql)
{
	// test
	cout << "mysql query sentence: " << sql << endl;

    mysqlpp::StoreQueryResult result;

    try
    {
        mysqlpp::Query query = _conn.query(sql);
        result = query.store();
    }
    catch(mysqlpp::Exception &ex)
    {
        _query_error = 1;
    }

    return result;
}

int MysqlProxy::IsQueryError()
{
    int ret = _query_error;
    _query_error = 0;

    return ret;
}

mysqlpp::StoreQueryResult MysqlProxy::SelectItemByID(string table_name, string id_name, string id_value)
{
	string sql = "select * from " + table_name + " where `" + id_name + "` = '" + id_value + "'";

	return DoQuery(sql);
}

int MysqlProxy::SingleRes2StdString(mysqlpp::StoreQueryResult &res, string &str, char splitChar)
{
	if(1 != res.num_rows())
	{
		return 1;
	}

	str = "";

	string tmpStr;

	for(size_t i = 0; i < res[0].size(); i++)
	{
		tmpStr = res[0][i].c_str();
		str = str + tmpStr;
		str = str + splitChar;
	}

	str.erase(str.length() - 1, 1);

	cout << __FUNCTION__ << str << endl;

	return 0;
}

void MysqlProxy::MultiRes2StdString(mysqlpp::StoreQueryResult &res, string &str,
	char splitChar, char wordChar)
{
	str = "";

    if(0 == res.num_rows())
    {
        return;
    }

	for(unsigned int i = 0; i < res.num_rows(); i++)
	{
		string item_str = "";

		for(size_t j = 0; j < res[i].size(); j++)
		{
		    cout << res[i][j].c_str() << endl;
			item_str = item_str + res[i][j].c_str();
			cout << item_str << endl;
			item_str = item_str + wordChar;
			cout << item_str << endl;
		}

		cout << "in MultiRes2StdString " << item_str << endl;

		item_str.erase(item_str.length() - 1, 1);
		str = str + item_str;
		str = str + splitChar;
	}

    if(1 >= str.length())
    {
        str = "";
    }
    else
    {
        str.erase(str.length() - 1, 1);
    }

    cout << __FUNCTION__ << ": " << str << endl;
}
